import com.entity.Book;
import com.entity.Shopingcar;
import com.mapper.BookMapper;
import com.mapper.ShopingcarMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by webrx on 2017/6/16.
 */
public class Dom02 {
    protected SqlSessionFactory sf;
    protected SqlSession ss;
    protected BookMapper bdao;
   /* protected ShopingcarMapper shop;*/
    @Before
    public void init(){

        try {
            this.sf=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));
            this.ss=this.sf.openSession();
            bdao= this.ss.getMapper(BookMapper.class);
            /*shop=this.ss.getMapper(ShopingcarMapper.class);*/
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @After
    public void  close(){
        this.ss.commit();
        this.ss.close();
    }
    @Test
    public void show(){
        System.out.println("************8");
    /* Shopingcar sh= new Shopingcar();
        sh.setCarcount(1);
        sh.setCarname("小强");
        sh.setCarprice(10);
        sh.setCarbianhao("10001");
        sh.setCarusername("大卫");
        shop.add(sh);*/


       /* Map<String,Object> map = new HashMap<String, Object>();
        map.put("bianhao","10001");
       List<Shopingcar> boo = shop.selectByBianHao(map);
        System.out.println(boo.size()>0);*/

        /*int nn = shop.updateCount("10001");
        System.out.println(nn);
*/

       /* Book bo = new Book();
        bo.setName("**");
        bo.setPrice(10000);
        bdao.add(bo);*/
        //逻辑分页

       /* RowBounds n =new RowBounds(0,3);
        List<Book> books =bdao.page02(n);
        for(Book boo :books){
            System.out.println(boo.getName());
        }*/
        //物理分页
      /* Map<String,Integer> map = new HashMap<String, Integer>();
        int a =1;
        int b =2;
        map.put("start",a*b-b);
        map.put("pagesize",b);
        List<Book> books1 =bdao.page(map);
        for(Book Bo: books1){
            System.out.println(Bo.getName());;
        }*/

        //快速分页
        /* int a=1;
         int b=2;
      List<Book> books= bdao.mypage(a,b );
      for(Book boo: books){
          System.out.println(boo.getName());
      }*/

    }
    @Test
    public  void sql(){
       /* Connection conn = ss.getConnection();
        String sql ="create database ddd";
        try {

            PreparedStatement pre= conn.prepareStatement(sql);
            pre.execute();
            pre.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }*/
    }
    @Test
    public  void  showDbs(){
        //用map集合查查数据库
       /* List<Map<String,Object>> maps = bdao.showDbs();
        for(Map<String,Object> mm: maps){
            System.out.println(mm.get("Database"));
        }*/

        //用list集合查询数据库
       /* List<String> list= bdao.showDbs2();
        for(String ss: list){
            System.out.println(ss);
        }*/
    }
    @Test
    public void  sss() {
    /*int nn= bdao.delbook(10);
        System.out.println(nn);*/

  /* Map<String,Object> m = new HashMap<String, Object>();
    m.put("name","北上广");
    m.put("price",66d);
    int num = bdao.addBook(m);
        System.out.println(num);*/

        //查询语句无参数
   /* List<Book> books =bdao.showbook();
    for(Book b :books){
        System.out.println(b.getName());
    }*/

        //有返回值的查询
   /*Map<String,Object> m = new HashMap<String,Object>();
        bdao.bookcc(m);
        System.out.println(m.get("r"));*/

  /* Map<String,Object> mm = new HashMap<String,Object>();
   bdao.bookcc(mm);
        System.out.println(mm.get("r"));*/

      /* List<Book> books = bdao.dongQuery();
       for(Book bb : books){
           System.out.println(bb.getName());
       }
*/


      /* Map<String,Object> map = new HashMap<String,Object>();
       map.put("name","%上%");
       map.put("price",505);
       List<Book> books1=bdao.dongQuery(map);
       for(Book boo: books1){
           System.out.println(boo.getName()+"@@@@");
       }*/

       /* Map<String,Object> map = new HashMap<String,Object>();
        map.put("name","大家好");
        map.put("id",12);
        int nn = bdao.dongupdate(map);
        System.out.println(nn);*/

      /*         Map<String,Object> map = new HashMap<String,Object>();
        map.put("name","%*%");
        map.put("price",22);
        List<Book> books= bdao.dongQuwhen(map);
        for(Book  book: books){
            System.out.println(book.getName());
        }*/

       /* Map<String,Object> map = new HashMap<String,Object>();
        map.put("name","你好");
       map.put("price",20d);
        List<Book> books= bdao.dongwhere(map);
        for(Book  book: books){
            System.out.println(book.getName());
        }*/

      /*  Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "xiao强");
        map.put("price", 19);*/
       /* map.put("id", 12);
        bdao.dongset(map);*/


       /* Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids",new int[]{12,13,14});
        List<Book> books =bdao.dongForeach(map);
        for(Book book: books){
            System.out.println(book.getName());
        }*/

        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("ids",new int[]{12,13});
        bdao.dongDelete(map1);
        String ss = "abcdefg";
        String aa ="cd";
        Pattern p =Pattern.compile(aa);
        Matcher m = p.matcher(ss);

        System.out.println(ss.lastIndexOf("de"));

        System.out.println(m.find());
        while(m.find()){
            System.out.println(m.group());
        }
    }


}
